package controller

import (
	models "fileshare/controller/modles"
	"fileshare/controller/server"
	"fileshare/controller/tools"
	"fileshare/utils"
	"net/http"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

/**
* @program: fileshare
*
* @description:
*
* @author: Koco.Allien
*
* @create: 2020-04-06 11:03
**/
func UserSelfController(c *gin.Context) {
	aid := tools.GatQuery(c, "active", 4)
	utils.MsgToLog("用户列表页面:UserSelfController")
	data, _, ok := tools.StartDate(c, "用户列表", aid)
	if !ok {
		data["error"] = "操作超出权限"
		c.HTML(http.StatusOK, "index.html", data)
	} else {
		users, count := server.GetUsers(-1, 100)
		data["users"] = users
		data["count"] = count
		c.HTML(http.StatusOK, "selfuser.html", data)
	}

}

//删除用户 fid={{$elem.ID}}&&active={{.active}}
func UserDelController(c *gin.Context) {
	aid := tools.GatQuery(c, "active", 4)
	utils.MsgToLog("用户列表页面:UserDelController")
	data, _, ok := tools.StartDate(c, "用户列表", aid)
	if !ok {
		data["error"] = "操作超出权限"
		c.HTML(http.StatusOK, "index.html", data)
	} else {
		uids := c.Query("uid")
		utils.MsgToLog("用户列表页面:UserDelController,uid", uids)
		if uid, err := strconv.Atoi(uids); err != nil {
			data["error"] = "操作超出权限"
			c.HTML(http.StatusOK, "index.html", data)
		} else {
			server.DelUser(uid)
			data["error"] = "删除成功"

			users, count := server.GetUsers(-1, 100)
			data["users"] = users
			data["count"] = count - 1
			c.HTML(http.StatusOK, "selfuser.html", data)
		}
	}
}

//更新用户的界面 uid={{$elem.ID}}&&active={{.active}}
func UserUpdateController(c *gin.Context) {
	aid := tools.GatQuery(c, "active", 5)
	data, _, ok := tools.StartDate(c, "用户列表", aid)
	if !ok {
		data["error"] = "操作超出权限"
		c.HTML(http.StatusOK, "index.html", data)
	} else {
		uid := tools.GatQuery(c, "uid", -1)
		user := server.GetUser(uid)
		data["uid"] = uid
		data["uname"] = user.Name
		c.HTML(http.StatusOK, "updateuser.html", data)
	}
}

//更新用户信息的处理事件
func UserUpdateControllerPost(c *gin.Context) {
	aid := tools.GatQuery(c, "active", 5)
	data, u, ok := tools.StartDate(c, "用户列表", aid)
	if !ok {
		data["error"] = "操作超出权限"
		c.HTML(http.StatusOK, "index.html", data)
	} else {
		msg := "更新成功"

		defer func() {
			users, count := server.GetUsers(-1, 100)
			data["users"] = users
			data["count"] = count - 1
			data["error"] = msg
			c.HTML(http.StatusOK, "selfuser.html", data)
		}()

		uname := c.PostForm("uname")
		//passwd := c.PostForm("passwd")
		passwd1 := c.PostForm("passwd1")
		passwd2 := c.PostForm("passwd2")
		utils.MsgToLog("更新用户信息的处理事件:UserUpdateControllerPost:", uname, passwd1, passwd2)
		uid := tools.GatQuery(c, "uid", -1)
		if uid == -1 {
			msg = "参数错误"
			return
		}
		//非管理员
		if u.ID != 2 {
			if uid != u.ID {
				msg = "权限不足"
				return
			}
			user := server.GetUser(uid)
			//需要准确的输入密码
			if len(uname) != 0 && passwd2 == passwd1 {

				var u *models.UserInfo
				if len(passwd2) == 0 {
					u = &models.UserInfo{
						ID:       uid,
						UpdateAt: time.Now().Local(),
						Name:     uname,
						Account:  user.Account,
						Password: user.Password,
						Role:     user.Role,
					}
				} else {
					u = &models.UserInfo{
						ID:       uid,
						UpdateAt: time.Now().Local(),
						Name:     uname,
						Account:  user.Account,
						Password: passwd2,
						Role:     user.Role,
					}
				}
				if err := server.Updateuser(u); err != nil {
					msg = "更新失败"
				}
				return
			}
			msg = "参数不合法"
			return

		} else {
			//管理员 随意输入密码
			user := server.GetUser(uid)
			if len(uname) != 0 && passwd2 == passwd1 {
				var u *models.UserInfo
				if len(passwd2) == 0 {
					u = &models.UserInfo{
						ID:       uid,
						UpdateAt: time.Now().Local(),
						Name:     uname,
						Account:  user.Account,
						Password: user.Password,
						Role:     user.Role,
					}
				} else {
					u = &models.UserInfo{
						ID:       uid,
						UpdateAt: time.Now().Local(),
						Name:     uname,
						Account:  user.Account,
						Password: passwd2,
						Role:     user.Role,
					}
				}

				if err := server.Updateuser(u); err != nil {
					msg = "更新失败"
				}
				return
			}
			msg = "参数不合法"
			return
		}

	}
}
